/**音频控制类
 * data 2011 5 11
 * author @zzy
 */
package cn.edu.dlut.WingGame.Sound;

import java.util.HashMap;

import android.content.SharedPreferences;
import cn.edu.dlut.WingGame.Game.WingGame;

public class SoundController {

	private static WingSound sound;
	private static String musicFile;
	private static boolean isMute = false;
	private static float musicVolume;
	private static HashMap<Integer, SoundFile> soundMap;
	private static final String settingFile = "soundSettings";

	public SoundController() {
		sound = new WingSound();

		SharedPreferences preferences = WingGame.getActivity()
				.getSharedPreferences(settingFile, 0);
		WingSound.mute = preferences.getBoolean("soundMute", false);
		isMute = preferences.getBoolean("musicMute", false);
		sound.setVolume(preferences.getFloat("soundVolume", 1));
		musicVolume = preferences.getFloat("musicVolume", 1);
	}

	public void loadSounds(int[] soundID, String[] soundName) {
		int length = soundID.length;
		soundMap = new HashMap<Integer, SoundFile>(length);

		for (int i = 0; i < length; i++) {
			soundMap.put(soundID[i], sound.createSoundFile("audio/"
					+ soundName[i]));
		}
	}

	public static void playSound(int id, boolean isLoop) {
		SoundFile sound = soundMap.get(id);
		if (isLoop) {
			sound.playContinuous();
		} else {
			sound.play();
		}
	}

	public static void playMusic(String file, boolean isLoop) {
		musicFile = file;
		if (!isMute) {
			WingMusic.stop();
			WingMusic.play(file, isLoop);
			WingMusic.getMediaPlayer().setVolume(musicVolume, musicVolume);
		}
	}

	public static void pauseMusic() {
		WingMusic.pause();
	}

	public static void resumeMusic() {
		WingMusic.play();
	}

	public static void muteSound(boolean mute) {
		WingSound.mute = mute;
		SharedPreferences preferences = WingGame.getActivity()
				.getSharedPreferences(settingFile, 0);
		preferences.edit().putBoolean("soundMute", mute).commit();
	}

	public static void muteMusic(boolean mute) {
		if (mute) {
			WingMusic.stop();
			WingMusic.getMediaPlayer().release();
		} else {
			WingMusic.play(musicFile, true);
			WingMusic.getMediaPlayer().setVolume(1, 1);
		}

		SharedPreferences preferences = WingGame.getActivity()
				.getSharedPreferences(settingFile, 0);
		preferences.edit().putBoolean("musicMute", mute).commit();
	}

	public static void setSoundVolume(float volume) {
		sound.setVolume(volume);
		SharedPreferences preferences = WingGame.getActivity()
				.getSharedPreferences(settingFile, 0);
		preferences.edit().putFloat("soundVolume", volume).commit();
	}

	public static void setMusicVolume(float volume) {
		WingMusic.getMediaPlayer().setVolume(volume, volume);
		SharedPreferences preferences = WingGame.getActivity()
				.getSharedPreferences(settingFile, 0);
		preferences.edit().putFloat("musicVolume", volume).commit();
	}

	public static void dealloc() {
		try {
			sound.destroy();
			WingMusic.stop();
			WingMusic.getMediaPlayer().release();
			soundMap.clear();
			sound = null;
			soundMap = null;
			musicFile = null;
		} catch (NullPointerException e) {
		}
	}

}
